package com.glodon.paas.monitor.web.controller;

import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.monitor.server.biz.domain.MonitorUserEntity;
import com.glodon.paas.monitor.server.dto.MonitorResultMsg;
import com.glodon.paas.monitor.server.exposed.facade.MonitorAppUserFacadeService;
import com.glodon.paas.monitor.web.constants.MonitorWebPageConstants;

@Controller
public class MonitorUserController {

	@Autowired
	private MonitorAppUserFacadeService	monitorAppUserFacadeService;

	@RequestMapping(value = "userMain", method = RequestMethod.GET)
	public ModelAndView indexUser(@RequestParam(value = "userName", required = false) String userName, ModelMap model,
			HttpServletRequest request) {

		boolean modelFlag = false;
		ModelMap doUserActionModel = (ModelMap) request.getSession().getAttribute("doUserModel");
		if (null != doUserActionModel && !doUserActionModel.isEmpty()) {
			modelFlag = true;
			userName = (String) doUserActionModel.get(StringConst.ITEM);
		}

		MonitorUserEntity userEntity = new MonitorUserEntity();
		if (StringUtils.isNotBlank(userName)) {
			userEntity.setUserName(userName);
		}

		MonitorResultMsg<ArrayList<MonitorUserEntity>> userResultMsg = monitorAppUserFacadeService
				.queryMonitorUser(userEntity);

		if (null != userResultMsg) {
			if (userResultMsg.isSuccess()) {
				ArrayList<MonitorUserEntity> appList = (ArrayList<MonitorUserEntity>) userResultMsg.getResultObject();
				model.addAttribute(StringConst.ITEMS, appList);
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, userResultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.QUERY_RESULT_EMPTY);
		}

		if (modelFlag) {
			model.addAttribute(StringConst.RETURN_MSG, doUserActionModel.get(StringConst.RETURN_MSG));
			model.addAttribute(StringConst.RESULT, doUserActionModel.get(StringConst.RESULT));
			request.getSession().removeAttribute("doUserModel");
		}

		return new ModelAndView("userMain", model);
	}

	@RequestMapping(value = "userAction", method = RequestMethod.GET)
	public ModelAndView indexUserAction(@RequestParam(value = "id", required = false) Long id, ModelMap model) {
		model.addAttribute("userId", id);
		if (id != null) {
			MonitorResultMsg<MonitorUserEntity> userMsg = monitorAppUserFacadeService.findMonitorUser(id);
			if (null != userMsg && userMsg.isSuccess()) {
				MonitorUserEntity userEntity = (MonitorUserEntity) userMsg.getResultObject();
				model.addAttribute("user", userEntity);
			}
		} else {
			MonitorUserEntity userEntity = new MonitorUserEntity();
			userEntity.setEmailLimitNum(50);
			userEntity.setPhoneLimitNum(20);
			userEntity.setPassword("123qwe!@#");
			model.addAttribute("user", userEntity);
		}
		return new ModelAndView("userAction", model);
	}

	@RequestMapping(value = "doUserAction", method = RequestMethod.POST)
	public ModelAndView doAppAction(@RequestParam(value = "userId", required = false) Long userId,
			@RequestParam(value = "userName", required = false) String userName,
			@RequestParam(value = "phoneNum", required = false) String phoneNum,
			@RequestParam(value = "phoneLimitNum", required = false) Integer phoneLimitNum,
			@RequestParam(value = "email", required = false) String email,
			@RequestParam(value = "number", required = false) Integer number,
			@RequestParam(value = "password", required = false) String password,
			@RequestParam(value = "isEmergencyUser", required = false) Integer isEmergencyUser,
			@RequestParam(value = "userType", required = false) Integer userType, ModelMap model,
			HttpServletRequest request) {
		if (null != userId) {
			MonitorResultMsg<MonitorUserEntity> resultMsg = monitorAppUserFacadeService.findMonitorUser(userId);
			if (null != resultMsg && resultMsg.isSuccess()) {
				MonitorUserEntity entity = (MonitorUserEntity) resultMsg.getResultObject();
				entity.setUpdateTime(new Date());
				entity.setIsEmergencyUser(isEmergencyUser);
				entity.setPassword(password);
				entity.setPhoneNum(phoneNum);
				entity.setPhoneLimitNum(phoneLimitNum);
				entity.setUserEmail(email);
				entity.setEmailLimitNum(number);
				entity.setUserName(userName);
				entity.setUserType(userType);
				MonitorResultMsg<MonitorUserEntity> appResultMsg = monitorAppUserFacadeService
						.updateMonitorUser(entity);
				if (null != appResultMsg) {
					if (appResultMsg.isSuccess()) {
						model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
						model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.UPDATE_SUCCESS);
					} else {
						model.addAttribute(StringConst.RESULT, StringConst.FAIL);
						model.addAttribute(StringConst.RETURN_MSG, appResultMsg.getResultMsg());
					}
				} else {
					model.addAttribute(StringConst.RESULT, StringConst.FAIL);
					model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.UPDATE_FAIL);
				}
			}
		} else {
			MonitorUserEntity entity = new MonitorUserEntity();
			entity.setCreateTime(new Date());
			entity.setIsEmergencyUser(isEmergencyUser);
			entity.setPassword(password);
			entity.setPhoneNum(phoneNum);
			entity.setPhoneLimitNum(phoneLimitNum);
			entity.setUserEmail(email);
			entity.setEmailLimitNum(number);
			entity.setUserName(userName);
			entity.setUserType(userType);
			MonitorResultMsg<MonitorUserEntity> appUserResultMsg = monitorAppUserFacadeService.saveMonitorUser(entity);
			if (null != appUserResultMsg) {
				if (appUserResultMsg.isSuccess()) {
					model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
					model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CREATE_SUCCESS);
				} else {
					model.addAttribute(StringConst.RESULT, StringConst.FAIL);
					model.addAttribute(StringConst.RETURN_MSG, appUserResultMsg.getResultMsg());
				}
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CREATE_FAIL);
			}
		}
		model.addAttribute(StringConst.ITEM, userName);
		request.getSession().setAttribute("doUserModel", model);
		return new ModelAndView(new RedirectView("userMain"));
	}

	@RequestMapping(value = "deleteUser", method = RequestMethod.GET)
	public ModelAndView doUserDelete(@RequestParam("id") Long id, ModelMap model, HttpServletRequest request) {
		if (null == id) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.INVALID_PARAMETER);
			request.getSession().setAttribute("doUserModel", model);
			return new ModelAndView(new RedirectView("userMain"));
		}
		MonitorResultMsg<Boolean> resultMsg = monitorAppUserFacadeService.deleteMonitorUser(id);
		if (null != resultMsg) {
			if (resultMsg.isSuccess()) {
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.DELETE_SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.DELETE_FAIL);
		}
		request.getSession().setAttribute("doUserModel", model);
		return new ModelAndView(new RedirectView("userMain"));
	}
}
